Date.prototype.format = function(format) {
	var date = {
		"M+" : this.getMonth() + 1,
		"d+" : this.getDate(),
		"h+" : this.getHours(),
		"m+" : this.getMinutes(),
		"s+" : this.getSeconds(),
		"q+" : Math.floor((this.getMonth() + 3) / 3),
		"S+" : this.getMilliseconds()
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + '')
				.substr(4 - RegExp.$1.length));
	}
	for ( var k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k]
					: ("00" + date[k]).substr(("" + date[k]).length));
		}
	}
	return format;
}

jQuery.validator.addMethod("minNumber", function(value,
		element) {
	var returnVal = true;
	inputZ = value;
	var ArrMen = inputZ.split("."); //截取字符串
	if (ArrMen.length == 2) {
		if (ArrMen[1].length > 2) {
			returnVal = false;
			return false;
		}
	}
	return returnVal;
}, "小数点后最多为两位");

if (typeof String.prototype.startsWith != 'function') {
	String.prototype.startsWith = function (prefix){
		return this.slice(0, prefix.length) === prefix;
    };
}

function getThumbnailUrl(originalUrl) {
	return originalUrl + '!s250';
}

function getOriginalUrl(thumbnailUrl) {
	return thumbnailUrl.replace('!s250', '');
}

function priceFormat(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	return (value / 100).toFixed(2); // .replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,'$&,');
}

function areaFormat(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	return (value / 100).toFixed(2); // .replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,'$&,');
}

function formatImage(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	return '<img width="62" height="62" src="' + getThumbnailUrl(value) + '" onclick="previewImg(this)" />';
}

function formatImageBase64(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	return '<img width="62" height="62" src="' + value+ '" onclick="previewImg(this)" />';
}

function formatImageGroup(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	var html = '<div class="img-group">';
	var imgUrls = value.split(',');
	for (var i in imgUrls) {
		html += '<img width="62" height="62" src="' + getThumbnailUrl(imgUrls[i]) + '" onclick="previewImg(this)" />';
	}
	html += '</div>';
	return html;
}

function formatVideo(value) {
	if ($.common.isEmpty(value)) {
		return '-';
	}
	return '<img width="62" height="62" src="/img/video-cover.jpg" onclick="previewVideo(\'' + value + '\')" />';
}

function dateFormat(value) {
    return value ? new Date(value).format("yyyy-MM-dd") : "";  
}
function datetimeFormat(value, fmt="yyyy-MM-dd hh:mm:ss") {
    return value ? new Date(value).format(fmt) : "";  
}

//金钱正则表达式校验
function checkMoney(value) {
	var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
	if (reg.test(value)) {
		return true;
	} else {
		return false;
	}
}

layui.use([ 'layer' ], function() {
	var $ = layui.$, layer = layui.layer;
	
	window.previewImg = function(obj) {
	    var img = new Image();  
	    img.src = getOriginalUrl(obj.src);
	    
	    $.modal.loading("加载中，请稍后...");
	    
	    img.onload = function() {
	    	$.modal.closeLoading();
	    	var height = img.height; //获取图片高度
		    var width = img.width; //获取图片宽度
		    if (height > document.documentElement.clientHeight * 0.8) {
		    	height = document.documentElement.clientHeight * 0.8;
		    	width = height * img.width / img.height;
		    }
		    var imgHtml = "<img width='"+width+"' height='"+height+"' src='" + getOriginalUrl(obj.src) + "' />";  
		    //弹出层
		    layer.open({  
		        type: 1,  
		        id: 'imgPopup',
		        shade: 0.8,
		        offset: 'auto',
		        area: [width + 'px',height+50+'px'],
		        shadeClose:true,//点击外围关闭弹窗
		        scrollbar: false,//不现实滚动条
		        title: "图片预览", //不显示标题  
		        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响  
		        success: function(layero, index){
//		        	var value = 0;
//		        	$("#imgPopup img").unbind("click").click(function(e){
//		        		value += 90;
//		        		$('#imgPopup img').rotate({
//		                    animateTo: value
//		                });
//		        		var w = 0;
//		        		var h = 0;
//		        		if ((value / 90) % 2 == 0) {
//		        			w = width + 'px';
//		        			h = height+50+'px';
//		        		} else {
//		        			h = width +50 + 'px';
//		        			w = height+'px';
//		        		}
//		        		layer.style(index, {
//		        			width: w,
//		        			height: h,
//		        			offset: 'auto'
//		        		});
//		        	});
		        },
		        cancel: function () {  
		            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });  
		        }
		    }); 
        }
	}
	
	window.previewVideo = function (src) {
        var videoHtml = "<video width='600' src='" + src + "' controls autoplay />";
        //弹出层
        layer.open({
            type: 1,
            title: "查看视频",
            shade: 0.8,
            offset: 'auto',
            area: '600px',
            shadeClose: true, //点击外围关闭弹窗
            scrollbar: false, //不现实滚动条
            content: videoHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响  
            success: function (layero, index) {
                layer.iframeAuto(index);
            },
            cancel: function () {
                //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });  
            }
        });
    }
})